package com.ramos.kuaixiao.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.db.BrandInfoDB;
import com.ramos.kuaixiao.db.PriceDB;
import com.ramos.kuaixiao.entity.BrandInfo;
import com.ramos.kuaixiao.entity.CartInfo;
import com.ramos.kuaixiao.entity.SendOrderDetail;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class SendOrderAdapter extends BaseAdapter {

    private List<CartInfo> cartlist= new ArrayList<>();
    private List<BrandInfo> tasklist= new ArrayList<>();
    private List<SendOrderDetail> detaillist = new ArrayList<>();
    private Context context;
    DecimalFormat df = new DecimalFormat("######0.00");
    static class ViewHolder {
        TextView mWName;
        LinearLayout order_ll;
        TextView mMoney;
        //EditText mComment;
    }

    public SendOrderAdapter(Context context, List<CartInfo> list) {
        this.context = context;
        this.cartlist = list;
    }

    public List<SendOrderDetail> getList(String customerName, String customerId) {
        detaillist.clear();
        BrandInfoDB brandInfoDB = new BrandInfoDB();
        List<BrandInfo> tasklist = brandInfoDB.findAllCustomerShop(App.mUserId,customerName);
        for(int position = 0 ; position < tasklist.size(); position++){
            SendOrderDetail orderDetail = new SendOrderDetail();
            orderDetail.setProductInfoId(tasklist.get(position).getEncode());
            orderDetail.setProductBarCode(tasklist.get(position).getBarcode());
            orderDetail.setStoreHouseId(tasklist.get(position).getWarehouseId());
            orderDetail.setStoreHouseName(tasklist.get(position).getWarehouse());
            orderDetail.setProductInfoName(tasklist.get(position).getShop_Name());
            orderDetail.setCalcParam(tasklist.get(position).getParam() + "");
            orderDetail.setSmallUnitCount(tasklist.get(position).getNum_Single() + "");
            orderDetail.setLargeUnitCount(tasklist.get(position).getNum_Box() + "");
            orderDetail.setUnitSalePrice(tasklist.get(position).getPrice() + "");
            orderDetail.setProductSalePrice(String.valueOf(tasklist.get(position).getPrice() * (tasklist.get(position).getNum_Box() * tasklist.get(position).getParam() + tasklist.get(position).getNum_Single()) + ""));
            /*for(int j = 0 ; j < cartlist.size(); j ++){
                if(orderDetail.getStoreHouseName().equals(cartlist.get(j).getWarehouse())){
                    orderDetail.setComment(cartlist.get(j).getComment());
                }
            }*/
            orderDetail.setLast_discount(new PriceDB().findLastDiscount(tasklist.get(position).getEncode(),customerId));
            orderDetail.setOrderSource(2);
            orderDetail.setUcti(tasklist.get(position).getUcti());
            orderDetail.setUcte(tasklist.get(position).getUcte());
            orderDetail.setSalePriceT4(tasklist.get(position).getSalePriceT4());
            orderDetail.setRealUcti(tasklist.get(position).getRealUcti());
            orderDetail.setRealUcte(tasklist.get(position).getRealUcti());
            orderDetail.setTcti(tasklist.get(position).getTcti());
            orderDetail.setTcte(tasklist.get(position).getTcte());
            orderDetail.setDiscountRate(100.0);
            orderDetail.setTaxRate(tasklist.get(position).getTaxRate());
            orderDetail.setTaxAmount(tasklist.get(position).getTaxAmount());
            orderDetail.setInitPrice(tasklist.get(position).getInitPrice());
            Log.d("SendOrderActivity", orderDetail.toString());
            detaillist.add(orderDetail);
        }
        return detaillist;
    }

    public void refresh(List<CartInfo> data) {
        cartlist = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return cartlist.size();
    }

    @Override
    public Object getItem(int position) {
        return cartlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.listitem_sendorder, null);
            holder = new ViewHolder();
            holder.mWName = (TextView) convertView.findViewById(R.id.name);
            //holder.mNum = (TextView) convertView.findViewById(R.id.num);
            holder.order_ll = (LinearLayout) convertView.findViewById(R.id.order_ll);
            holder.mMoney = (TextView) convertView.findViewById(R.id.price);
            //holder.mComment = (EditText) convertView.findViewById(R.id.comment);

            class MyTextWatcher implements TextWatcher {
                public MyTextWatcher(ViewHolder holder) {
                    mHolder = holder;
                }

                private ViewHolder mHolder;

                @Override
                public void onTextChanged(CharSequence s, int start,
                                          int before, int count) {
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start,
                                              int count, int after) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (s != null && !"".equals(s.toString())) {

                        cartlist.get(position).setComment(s.toString());// 当EditText数据发生改变的时候存到data变量中
                    }
                }
            }
            //holder.mComment.addTextChangedListener(new MyTextWatcher(holder));
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Log.d("OrderDetail ", "position " +  position);
        if(position == 0 ){
            detaillist.clear();
        }
        holder.mWName.setText(cartlist.get(position).getWarehouse()+"(" +  cartlist.get(position).getStoreNum() + ")");
        //holder.mNum.setText("商品(" +  cartlist.get(position).getStoreNum() + ")");
        holder.mMoney.setText("￥"+ df.format(cartlist.get(position).getPrice()));

        for (BrandInfo p :  cartlist.get(position).getBrandInfos()) {
            //1.集合中每有一条元素，就new一个textView
            TextView tv = new TextView(context);
            //2.把信息设置为文本框的内容
            tv.setText(p.getShop_Name() + "("+p.getNum_Box() + "箱" + p.getNum_Single() +  "支)");
            tv.setPaddingRelative(25,0,0,10);
            tv.setTextSize(14);
            //3.把textView设置为线性布局的子节点
            holder.order_ll.addView(tv);
        }

        return convertView;
    }
}